<?php

/**
 * MLEPP - ManiaLive Extending Plugin Pack
 *
 * -- MLEPP Plugin --
 * @name AdminGroups
 * @date 25-06-2011
 * @version r934
 * @website mlepp.com
 * @package MLEPP
 *
 * @author The MLEPP team
 * @copyright 2010 - 2011
 *
 * ---------------------------------------------------------------------
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------
 * You are allowed to change things or use this in other projects, as
 * long as you leave the information at the top (name, date, version,
 * website, package, author, copyright) and publish the code under
 * the GNU General Public License version 3.
 * ---------------------------------------------------------------------
 */

namespace ManiaLivePlugins\MLEPP\Core;

use ManiaLive\Data\Storage;
use ManiaLive\DedicatedApi\Connection;
use ManiaLive\Utilities\Console;

class ChatRedirect extends \ManiaLib\Utils\Singleton {

    private $storage;
    private $connection;
    public $mlepp;
    private $callbacks = array();
    private $hide = array();

    protected function __construct() {
        $this->storage = Storage::getInstance();
        $this->connection = Connection::getInstance();
    }

    /**
     * sendChat()
     * Function used for sending text to the server.
     *
     * @param mixed $text
     * @param string $login
     * @return void
     */
    public function sendChat($text, $login = false, $sender = null) {

        //Check if chat needs to be redirected.
        if ($sender != null) {
            if (is_object($sender)) {
                $senderName = get_class($sender);
            }
            else
                $senderName = $sender;


            //Was a redirect configured
            if (isset($this->callbacks[$senderName])) {

                //Do the callBack
                foreach ($this->callbacks[$senderName] as $callback) {
                    $loginString = false;
                    if ($login) {
                        if (isset($login->login)) {
                            $loginString = $login->login;
                        } else {
                            $loginString = $login;
                        }
                    }
                     \call_user_func_array($callback, array($text, $loginString));
                    }

                    //Check if still needs to be sent throught normal chat.
                    if (isset($this->hide[$senderName]) && $this->hide[$senderName] == true) {
                        return;
                    }
                }
            }

            try {
                if ($login) {
                    if (isset($login->login)) {
                        $loginObj = $login;
                    } else {
                        $loginObj = $this->storage->getPlayerObject($login);
                    }
                    $this->connection->chatSendServerMessage('$fff» ' . $this->mlepp->parseColors($text), $loginObj);
                } else {
                    //$this->log('[SERVER] ' . $this->parseColors($text));
                    $this->connection->chatSendServerMessage('$fff»» ' . $this->mlepp->parseColors($text));
                }
            } catch (\Exception $e) {
                Console::println('[MLEPP] A General error occurred when sending message to game:' . $e->getMessage() . "\n" . $text);
            }
        }

        public function registerRedirect($redirectedObject, $object, $function) {

        if (!\is_string($redirectedObject))
        $redirectedObject = \get_class($redirectedObject); else {
        $exploded = \explode('\\', $redirectedObject);
        if ($exploded[0] != 'ManiaLivePlugins') {
        $redirectedObject = 'ManiaLivePlugins\\' . $redirectedObject;
    }
}
$this->callbacks[$redirectedObject][] = array($object, $function);
}

public function deleteRedirect($redirectedObject, $object, $function) {

if (!\is_string($redirectedObject))
    $redirectedObject = \get_class($redirectedObject);
else {
    $exploded = \explode('\\', $redirectedObject);
    if ($exploded[0] != 'ManiaLivePlugins') {
        $redirectedObject = '\ManiaLivePlugins\\' . $redirectedObject;
    }
}

if (isset($this->callbacks[$redirectedObject])) {
    foreach ($this->callbacks[$redirectedObject] as $key => $callback) {

        if ($callback[0] == $object && $callback[1] == $function) {
            unset($this->callbacks[$redirectedObject][$key]);
        }
    }
}
}

public function setHideChat($redirectedObject) {
if (!\is_string($redirectedObject))
    $redirectedObject = \get_class($redirectedObject);
else {
    $exploded = \explode('\\', $redirectedObject);
    if ($exploded[0] != 'ManiaLivePlugins') {
        $redirectedObject = 'ManiaLivePlugins\\' . $redirectedObject;
    }
}

if (isset($this->callbacks[$redirectedObject])) {
    $this->hide[$redirectedObject] = true;
} else {
    Console::println('[MLEPP]You can not ask a chat to be hidden before redirecting it');
}
}

}

?>
